package com.tairanchina.joat;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by 周靖捷 on 2016/10/17.
 */
public class GenericPluginProgressManager implements PluginProgressManager {
    private final PluginManager pluginManager;
    ConcurrentHashMap<PluginMeta, Plugin> progressMap = new ConcurrentHashMap<PluginMeta, Plugin>();

    public GenericPluginProgressManager(PluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }

    public Plugin instance(PluginMeta pluginMeta) {
        if (progressMap.containsKey(pluginMeta)) {
            return progressMap.get(pluginMeta);
        }
        Plugin plugin = pluginManager.instance(pluginMeta.getPluginId());
        progressMap.put(pluginMeta, plugin);
        return plugin;
    }

    public void kill(PluginMeta pluginMeta) {
        progressMap.remove(pluginMeta);
    }
}
